#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>

#include <sw/redis++/redis++.h>

#include "util.hpp"

using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::unordered_map;

namespace redis = sw::redis;

void test_zadd_and_zrange(redis::Redis& redis){
    cout << "test zadd and zrange" << endl;
    redis.flushall();

    // 添加一个元素
    redis.zadd("key1", "zhangsan", 100);
    // 添加多个元素
    redis.zadd("key1", {
        std::make_pair("lisi", 99),
        std::make_pair("wangwu", 78)
    });
    // 容器
    vector<std::pair<string, double>> vec = {
        {"zhaoliu", 88},
        {"Amy", 66}
    };
    redis.zadd("key1", vec.begin(), vec.end());

    // 获取
    // 如果输出容器是单 string 类型，则只有 member
    // 如果输出容器是 string 和 double 类型，则会输出 member 和 score
    vector<string> members;
    auto it1 = std::back_inserter(members);
    redis.zrange("key1", 0, -1, it1);
    printContainer(members);

    vector<std::pair<string, double>> values;
    auto it2 = std::back_inserter(values);
    redis.zrange("key1", 0, -1, it2);
    for(auto& e : values){
        cout << e.first << " " << e.second << endl;
    }
}

void test_zcard_and_zrem(redis::Redis& redis){
    cout << "test zcard and zrem" << endl;
    redis.flushall();

    redis.zadd("key1", {
        std::make_pair("zhangsan", 100),
        std::make_pair("lisi", 99),
        std::make_pair("wangwu", 78)
    });
    
    // 获取个数
    auto num = redis.zcard("key1");
    cout << "zcard num: " << num << endl;

    // 删除
    auto count = redis.zrem("key1", {"zhangsan", "lisi"});
    cout << "zrem num: " << count << endl;
}

void test_zscore_and_zrank(redis::Redis& redis){
    cout << "test zscore and zrank" << endl;
    redis.flushall();

    // 添加
    redis.zadd("key1", {
        std::make_pair("zhangsan", 100),
        std::make_pair("lisi", 99),
        std::make_pair("wangwu", 78)
    });

    // 获取
    auto score1 = redis.zscore("key1", "zhangsan");
    if(score1){
        cout << "zhangsan score: " << *score1 << endl;
    }
    else{
        cout << "zhangsan score not found" << endl;
    }

    // 排名
    auto rank = redis.zrank("key1", "zhangsan");
    if(rank){
        cout << "zhangsan rank: " << *rank << endl;
    }
    else{
        cout << "zhangsan rank not found" << endl;
    }

}

int main(){
    redis::Redis redis("tcp://666666@127.0.0.1:6379");

    // test_zadd_and_zrange(redis);
    // test_zcard_and_zrem(redis);
    test_zscore_and_zrank(redis);

}